using ValidationFramework.Extensions;
using ValidationFramework.Configuration.Xml;
using ValidationFramework.Configuration;

namespace ValidationFramework.Specifications
{
    /// <summary>
    /// A <see cref="IRuleConfigReader"/> that creates a <see cref="LengthStringRule"/> from a <see cref="RuleData"/>.
    /// </summary>
    /// <remarks>
    ///   <see cref="RuleData"/> for default attributes.
    ///   <b>Extra Attributes</b>
    ///   <ul>
    ///     <li>
    ///       <c>minimum</c> (optional): Used to populate <see cref="ValidationFramework.LengthRule.Maximum"/>.   
    ///     </li>
    ///     <li>
    ///       <c>maximum</c> (required): Used to populate <see cref="ValidationFramework.LengthRule.Minimum"/>.   
    ///     </li>
    ///   </ul>
    /// </remarks>
    /// <example>
    /// <code lang="xml" title="This example shows an xml configuration for LengthStringRuleConfigReader">
    /// <rule 
    /// errorMessage="hello" 
    /// typeName="LengthStringRule" 
    /// minimum="1" 
    /// maximum="5"/>";
    /// </code>
    /// </example>
    /// <seealso cref="LengthStringRule"/>
    /// <seealso cref="LengthStringRuleAttribute"/>
    /// <seealso cref="ConfigurationService"/>
    public sealed class HasLengthInRangeParser : ISpecificationXmlParser
    {
		public ISpecification GetSpecification(System.Xml.XmlElement SpecificationElement, RuleContext context)
		{
			Guard.ArgumentNotNull(SpecificationElement, "SpecificationElement");

			var minimum = SpecificationElement.GetAttribute("minimum", 0);
			var maximum = SpecificationElement.GetAttribute<int>("maximum");
			var trimWhiteSpace = SpecificationElement.GetAttribute("trimWhiteSpace", true);
			return new HasLengthInRangeSpec(minimum, maximum, trimWhiteSpace);
		}
	}
}